<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="~{include :: header('书籍列表')}" />
    <th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<div class="container-fluid mb-2">
    <div class="toolbar" id="toolbar" role="group">
        <form id="formId">
            <div class="form-inline">
                <a class="btn btn-success mr-1" id="btn-book-add">
                    <i class="fa fa-plus"></i> 添加书籍
                </a>
                <input type="text" class="form-control mr-1" name="searchKey" placeholder="请输入标题或书籍id...">
                <button type="button" class="btn btn-primary mr-1" onclick="$.table.search()">
                    <i class="fa fa-search"></i>
                    搜索
                </button>
                <button type="button" class="btn btn-default" onclick="$.form.reset()">
                    <i class="fa fa-refresh"></i>
                    重置
                </button>
                <input type="hidden" name="channelId" th:value="${channelId}"/>
            </div>
        </form>
    </div>
    <table id="bootstrap-table" class="table-striped"></table>
</div>
<th:block th:insert="~{include :: footer}" />
<th:block th:insert="~{include :: bootstrap-table-js}" />
<script th:inline="javascript">
    var prefix = "/book";
    var channelId = [[${channelId}]];

    $(function() {
        var options = {
            url: prefix + "/list.json",
            createUrl: prefix + "/add.html",
            updateUrl: prefix + "/edit.html?id={id}",
            modalName: "书籍",
            columns: [
                {
                    field: 'id',
                    title: 'id',
                    width: 60
                },
                {
                    field: 'cover',
                    title: '封面',
                    align: 'center',
                    width: 80,
                    formatter: function(value, row, index) {
                        if (value === "" || value == null) {
                            return "<img class='img-circle img-xs' data-height='50' data-width='50' data-target='self' src='/static/logo-big.png'/>"
                        } else {
                            return $.common.sprintf("<img class='img-circle img-xs' height='50' width='50' data-target='self' src='%s'/>",  value);
                        }
                    }
                },
                {
                    field: 'title',
                    title: '名称'
                },
                {
                    field: 'categoryName',
                    title: '分类',
                    width: 120
                },

                {
                    field: 'price',
                    title: '价格',
                    width: 80,
                    align: 'right',
                    formatter: function (value, row, index) {
                        return value / 100;
                    }
                },
                {
                    field: 'buyCount',
                    align: 'center',
                    title: '销量',
                    width: 80
                },
                {
                    field: 'viewCount',
                    align: 'center',
                    title: '浏览',
                    width: 80
                },
                {
                    field: 'updateTime',
                    title: '更新时间',
                    width: 180
                },
                {
                    field: 'isAvailable',
                    align: 'center',
                    title: '上架',
                    width: 80,
                    formatter: function (value, row, index) {
                        return isAvailableTools(row);
                    }
                },
                {
                    title: '操作',
                    align: 'left',
                    width: 180,
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="bookView(\'' + row.id + '\')"><i class="fa fa-search"></i>详情</a> ');
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

        //添加书籍
        $('#btn-book-add').click(function (){
            $.modal.openTab("新增书籍", prefix + "/add.html?channelId=" + channelId);
        })
    });

    /* 状态显示 */
    function isAvailableTools(row) {
        if (row.isAvailable === 1) {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="updateIsAvailable(\'' + row.id + '\',\'' + 0 + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="updateIsAvailable(\'' + row.id + '\',\'' + 1 + '\')"></i> ';
        }
    }

    //上架与下架
    function updateIsAvailable(id, status) {
        table.set();
        var str = "";
        if (status > 0) {
            str = "确定执行上架操作"
        } else {
            str = "确定执行下架操作"
        }
        $.modal.confirm(str, function () {
            var data = {"id": id, "isAvailable": status};
            $.operate.submit(prefix + "/update/available.json", "post", "json", data);
        });
    }

    //详情
    function bookView(id) {
        var index = layer.open({
            title: '书籍详情',
            content:'/book/view.html?id='+id,
            type: 2,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            area: ['70%', '90%']
        });
    }
</script>
</body>
</html>